/**
 * 2017年6月25日
 */
package org.textplantform.common.core;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

import org.apache.commons.io.FileUtils;
import org.textplantform.common.Constant;
import org.textplantform.common.provider.NetworkModelService;

import com.alibaba.dubbo.common.utils.IOUtils;

/**
 * @author Alex
 *
 */
public class NetworkModelManager extends LoggerSupport implements NetworkModelService {
	Properties p = new Properties();
	Date lastModitiedDate = null;
	Calendar calendar = Calendar.getInstance();
	byte [] modelByte = null;
	public NetworkModelManager(){
		//默认4M
		ByteArrayOutputStream baos = new ByteArrayOutputStream(4*1024);
		try {
			p.load(this.getClass().getClassLoader().getResourceAsStream("meta/"+Constant.Values.ConfigFileName));
			IOUtils.write(this.getClass().getClassLoader().getResourceAsStream("data/"+Constant.Values.ModelFileName), baos);
			modelByte = baos.toByteArray();
		} catch (IOException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
		calendar.set(2000, 12, 12);
		lastModitiedDate = calendar.getTime();
		logger.info("网络模型最近修改时间: {} "+lastModitiedDate);
	}
	/**
	 * 调用的时候判断客户端缓存的时间如果小于RPC返回时间,则获取流
	 * 		//返回一个旧日期
	 */
	@Override
	public Date lastModifiedTime() {
		return lastModitiedDate;
	}

	@Override
	public byte[] getModelFileStream() {
		return modelByte;
	}

	@Override
	public Deep4jModelType getType() {
		return p.getProperty("CNN_TYPE").equals("CG")?Deep4jModelType.ComputationGraph:Deep4jModelType.MultiLayerNetwork;
	}
	@Override
	public int getSentenceLength() {
		return Integer.parseInt(p.getProperty("MAX_SENTENCE_LENGTH"));
	}
}
